<!--
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-09-09 19:12:43
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-09-09 21:00:41
 * @FilePath: /XMate-h5/src/views/customerAndBusiness/components/competition/index.vue
 * @Description: 
 * 
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
-->
<template>
  <div class="p-4">
    <template v-if="!competitorsList.length">
      <div class="flex flex-row-reverse"></div>
      <van-empty description="暂无内容" />
    </template>

    <template v-else>
      <div class="flex flex-row items-center justify-between">
        <competitionNavTab v-model:active="activeIndex" :tabs="cp_tabs" class="flex-1" />
      </div>
      <competitionInfo :info="currentInfo" />
    </template>
  </div>
</template>

<script setup>
import competitionNavTab from "./competitionNavTab.vue";
import competitionInfo from "./competitionInfo.vue";
import { useCustomerStore, useBusinessStore } from "@/store";
import { ref, onMounted, computed, defineProps } from "vue";
import { formatCompletionTime, ParameterDateType } from "@/utils/timeTool";

const store = useCustomerStore();
const businessStore = useBusinessStore();
const activeIndex = ref(0);

const props = defineProps({
  isCustomer: {
    type: Boolean,
    default: true,
  },
});

onMounted(() => {
  const dateRange = store.getDefaultDateRange();
  getData(dateRange.st, dateRange.et);
});

const cp_tabs = computed(() => {
  return competitorsList.value.map((item) => {
    const meets = item.meets || [];
    const count = meets.reduce((sum, item) => {
      return sum + item.mentionCount;
    }, 0);
    return {
      title: item.commonName,
      count: count,
    };
  });
});

function getData(startTime, endTime) {
  if (props.isCustomer) {
    store.getCompetitorsInfoAction(startTime, endTime);
  } else {
    businessStore.getOpportunityCompetitorsInfoAction(startTime, endTime);
  }
}

const competitorsList = computed(() => {
  return props.isCustomer ? store.competitorsList : businessStore.competitorsList;
});

const cp_competitorsInfo = computed(() => {
  return props.isCustomer ? store.competitorsInfo : businessStore.competitorsInfo;
});

const currentInfo = computed(() => {
  if (!competitorsList.value) return null;

  const competitorsInfo = competitorsList.value[activeIndex.value];
  const comppetitorId = competitorsInfo.id;
  const meets = competitorsInfo.meets;

  let info = {};
  info.analysis = cp_competitorsInfo.value[comppetitorId];
  info.isEmpty = JSON.stringify(cp_competitorsInfo.value[comppetitorId]) === "{}";
  info.meets = meets;
  return info;
});
</script>

<style lang="less" scoped></style>
